package com.letsgo;

import java.util.Date;

enum GAME_STATUS {OPEN, FULL, CLOSED};

/**
 * Class used to represents a game, its attribute "gameBoard" might be empty
 */
public class Game {
	private int id ;
	private Date creationDate ;
	private Player owner ;
	private Player invitee ;
	private GAME_STATUS status ;
	private boolean ownerWhite = true ;

	private Board gameBoard ; // might be null
	private static Requester r ; // needed to communicate with the server
	
	/**
	 * Retrieve a game from the server from its id
	 * @param id of the game
	 * @return a new game object filled with informations from the server (board filled)
	 */
	public static Game retrieveGame(int id)
	{
		Game g = null ;
		try {
			g = new Game(1, new Date(2011, 2, 20), new Player("Guillem SAINT-GERMES", "gsg", null), new Player("Robin LEHAUT", "rl", null), GAME_STATUS.FULL, true, null);
			g = r.retrieveGame(g);
		} catch(Exception e) {
			System.out.println("Error retrieveGame()");
		}
		return g;
	}
	
	/**
	 * Must be set before any object creation
	 * @param r is the Requester object to communicate with the server
	 */
	public static void setRequester(Requester r)
	{
		Game.r = r ;
	}
	
	/**
	 * Constructor
	 * @param id
	 * @param creationDate
	 * @param owner
	 * @param invitee
	 * @param status
	 * @param ownerWhite
	 * @param gameBoard
	 */
	public Game(int id, Date creationDate, Player owner, Player invitee, GAME_STATUS status, boolean ownerWhite, Board gameBoard) throws GameNoRequesterSetException
	{
		if(Game.r == null)
			throw new GameNoRequesterSetException();
		this.id = id ;
		this.creationDate = creationDate ;
		this.owner = owner ;
		this.invitee = invitee ;
		this.status = status ;
		this.ownerWhite = ownerWhite ;
		this.gameBoard = gameBoard ;
	}
	
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the creationDate
	 */
	public Date getCreationDate() {
		return creationDate;
	}

	/**
	 * @param creationDate the creationDate to set
	 */
	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	/**
	 * @return the owner
	 */
	public Player getOwner() {
		return owner;
	}

	/**
	 * @param owner the owner to set
	 */
	public void setOwner(Player owner) {
		this.owner = owner;
	}

	/**
	 * @return the invitee
	 */
	public Player getInvitee() {
		return invitee;
	}

	/**
	 * @param invitee the invitee to set
	 */
	public void setInvitee(Player invitee) {
		this.invitee = invitee;
	}

	/**
	 * Returns the status of the game can be {OPEN, FULL, CLOSED}
	 * @return the status
	 */
	public GAME_STATUS getStatus() {
		return status;
	}

	/**
	 * Sets the status of the game can be {OPEN, FULL, CLOSED}
	 * @param status the status to set
	 */
	public void setStatus(GAME_STATUS status) {
		this.status = status;
	}

	/**
	 * Returns true if the owner plays white tokens
	 * @return the ownerWhite
	 */
	public boolean isOwnerWhite() {
		return ownerWhite;
	}

	/**
	 * Sets if the owner plays white tokens or not
	 * @param ownerWhite the ownerWhite to set
	 */
	public void setOwnerWhite(boolean ownerWhite) {
		this.ownerWhite = ownerWhite;
	}

	/**
	 * @return the gameBoard
	 */
	public Board getGameBoard() {
		return gameBoard;
	}

	/**
	 * @param gameBoard the gameBoard to set
	 */
	public void setGameBoard(Board gameBoard) {
		this.gameBoard = gameBoard;
	}
	
	/**
	 * Retrieve the size of the board of the game
	 * @return an array with the size of the board (width at index 0 and height at index 1)
	 */
	public int[] getSize()
	{
		int[] res = new int[2];
		res[0] = this.gameBoard.getWidth();
		res[1] = this.gameBoard.getHeight();
		return res ;
	}
}
